package tech.eyao.hs60x.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import tech.eyao.hs60x.redis.template.AccountCoRedisTemplate;
import tech.eyao.hs60x.redis.template.IntegerRedisTemplate;
import tech.eyao.hs60x.redis.template.LbsCoRedisTemplate;
import tech.eyao.hs60x.redis.template.ShakeCoRedisTemplate;

@Configuration
public class RedisConfig {

    @Value("${redis.host}")
    private String url;

    @Value("${redis.port}")
    private int    port;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        jedisConnectionFactory.setHostName(url);
        jedisConnectionFactory.setPort(port);
        jedisConnectionFactory.setPassword("hs60x");
        return jedisConnectionFactory;
    }

    @Bean
    public StringRedisTemplate redisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(jedisConnectionFactory());
        stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
        stringRedisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        return stringRedisTemplate;
    }
    
    @Bean(name = "lbsCoRedisTemplate")
    public LbsCoRedisTemplate prizeRedisTemplate() {
    	LbsCoRedisTemplate template = new LbsCoRedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
    
    @Bean(name = "accountCoRedisTemplate")
    public AccountCoRedisTemplate accountCoRedisTemplate() {
    	AccountCoRedisTemplate accountCoRedisTemplate = new AccountCoRedisTemplate();
    	accountCoRedisTemplate.setConnectionFactory(jedisConnectionFactory());
    	return accountCoRedisTemplate;
    }
    
    @Bean(name = "integerRedisTemplate")
    public IntegerRedisTemplate integerRedisTemplate() {
    	IntegerRedisTemplate integerRedisTemplate = new IntegerRedisTemplate();
    	integerRedisTemplate.setConnectionFactory(jedisConnectionFactory());
    	return integerRedisTemplate;
    }
    
    @Bean(name = "shakeCoRedisTemplate")
    public ShakeCoRedisTemplate shakeCoRedisTemplate() {
    	ShakeCoRedisTemplate shakeCoRedisTemplate = new ShakeCoRedisTemplate();
    	shakeCoRedisTemplate.setConnectionFactory(jedisConnectionFactory());
    	return shakeCoRedisTemplate;
    }
    
}
